;;;; iii.lisp

(in-package #:iii-impl)

(defvar *connection*)
(defvar *connections*)
(defvar *channel*)

(defvar *logging-stream* t)

(defclass iii-connection (irc:connection) ())

(defun log-hook (message)
  (irc:client-log *connection* message)
  t)

(defmethod irc:add-default-hooks ((connection iii-connection))
  (call-next-method)
  (dolist (message '(irc:irc-rpl_isupport-message
                      irc:irc-rpl_whoisuser-message
                      irc:irc-rpl_banlist-message
                      irc:irc-rpl_endofbanlist-message
                      irc:irc-rpl_exceptlist-message
                      irc:irc-rpl_endofexceptlist-message
                      irc:irc-rpl_invitelist-message
                      irc:irc-rpl_endofinvitelist-message
                      irc:irc-rpl_list-message
                      irc:irc-rpl_topic-message
                      irc:irc-rpl_namreply-message
                      irc:irc-rpl_endofnames-message
                      irc:irc-rpl_welcome-message
                      ; irc:irc-ping-message
                      irc:irc-join-message
                      irc:irc-topic-message
                      irc:irc-part-message
                      irc:irc-quit-message
                      irc:irc-kick-message
                      irc:irc-nick-message
                      irc:irc-mode-message
                      irc:irc-rpl_channelmodeis-message
                      irc:ctcp-time-message
                      irc:ctcp-source-message
                      irc:ctcp-finger-message
                      irc:ctcp-version-message
                      irc:ctcp-ping-message
                      irc:irc-privmsg-message
                      ))
    (irc:add-hook connection message 'log-hook)))

(defmethod irc:privmsg ((connection iii-connection) (target string) message)
  (irc:irc-message-event *connection* (make-instance 'irc:irc-privmsg-message
                                                     :source (irc:nickname (irc:user *connection*))
                                                     :command :privmsg
                                                     :connection *connection*
                                                     :arguments (list target message)
                                                     :received-time (get-universal-time)))

  (call-next-method))

(defun setup-irc (&key nickname username realname password server (logging-stream *logging-stream*))
  (when *connection*
    (ignore-errors (close (irc:network-stream *connection*))))
  (setf *connection* (irc:connect :connection-type 'iii-connection
                                  :nickname nickname
                                  :server server
                                  :logging-stream logging-stream
                                  :username username
                                  :realname realname
                                  :password password))
  (push *connection* *connections*)
  (bt:make-thread (lambda () (irc:read-message-loop *connection*)) :name "irc"))

(defparameter *part-reason* nil)
(defparameter *quit-message* nil)

(defun privmsg (message &key (channel *channel*) (connection *connection*))
  (irc:privmsg connection channel message))

(defun join (channel &key (connection *connection*) password)
  (irc:join connection channel :password password))

(defun part (channel &key (connection *connection*) (reason *part-reason*))
  (irc:part connection channel reason))

(defun quit (&key (connection *connection*) (message *quit-message*))
  (irc:quit connection message))
